//--------------------------------------------------------------------------------------------------------
// 
// Author: Zachary Gay
// Date: 8/23/2012
// Copyright (c) 2012 Zachary Gay.  All Rights Reserved.
//
// Filename: GameController.m
//
//--------------------------------------------------------------------------------------------------------

#import "GameController.h"

#import "Card.h"
#import <stdlib.h>
#import "Constants.h"
#import "ManaGroup.h"

#define kMaxCardsInHand 7

// Singleton Instance
static GameController *theGameController = nil;

@implementation GameController

@synthesize deck, hand, graveyard, board;
@synthesize manaPool;

#pragma mark - Constructor and Singleton Access Methods

- (GameController *) initWithNumberOfTurns:(int)_numberOfTurns
{
    if (self = [super init])
    {
        numberOfTurns = _numberOfTurns;
        currentTurn = 0;
    }
    
    return self;
}

+ (GameController *) theGameController
{
    if (theGameController == nil)
    {
        NSLog(@"ERROR: +theGameController called on unitialized GameController!");
        return nil;
    }
    
    return theGameController;
}

#pragma mark - Card Methods
#pragma mark - 

- (void) drawCard
{
    // Move random card from deck to hand
    int randomCardIndex = arc4random() % self.deck.count - 1;
    Card *drawnCard = [self.deck objectAtIndex: randomCardIndex];
    
    [self.deck removeObject: drawnCard];
    [self.hand addObject: drawnCard];
}

- (void) playCard: (Card *) _card
{
    for (Card *aCard in self.hand)
    {
        if (aCard == _card)
        {
            [self.hand removeObject: aCard];
            [self.board addObject: aCard];
            
            return;
        }
    }
    
    NSLog(@"ERROR: -playCard called and _card could not be found in hand!");
}

- (void) discardCard: (Card * ) _card
{
    for (Card *aCard in self.hand)
    {
        if (aCard == _card)
        {
            [self.hand removeObject: aCard];
            [self.graveyard addObject: aCard];
            
            return;
        }
    }
    
    NSLog(@"ERROR: -discardCard called and _card could not be found in hand!");
}

- (void) discardIfNeeded
{
    if (self.hand.count > kMaxCardsInHand)
    {
        // Find card with highest converted mana cost
        int highestConvertedManaCost = 0;
        Card *cardToDiscard = nil;
        for (Card *aCard in self.hand)
        {
            if ([aCard.cost convertedManaCost] > highestConvertedManaCost)
            {
                highestConvertedManaCost = [aCard.cost convertedManaCost];
                cardToDiscard = aCard;
            }
        }
        
        // Discard that card
        [self discardCard: cardToDiscard];
    }
}

#pragma mark - Turn Methods
#pragma mark - Helpers

- (void) untap
{
    for (Card *aCard in self.board)
    {
        [aCard untap];
    }
}

- (void) generateMana
{
    #warning Implement Me!
    // 'Tap' all lands and use all mana producers to generate all possible mana
    // NOTE: Since this is just for testing, we ignore mana burn
}

- (void) playLand
{
    #warning Implement Me!
    // Find the most useful land in hand and play it
}

- (void) playNonLand
{
    #warning Implement Me!
    // Starting with the card that has the smallest mana cost, attempt to play each card
}

- (void) mainPhase
{
    [self playLand];
    [self playNonLand];
}

- (void) endTurn
{
    #warning Implement Me!
    // Not sure what to do here.  Probably just going to be calls to statsController
}

#pragma mark - Main

- (void) playGame
{
    for (int i = 0; i < numberOfTurns; i++)
    {
        [self untap];
        [self mainPhase];
        [self endTurn];
    }
}

#pragma mark - Game Methods
#pragma mark - Helpers

- (void) readyNextGame
{
    // Cleanup everything from the old game and get the next game ready to go
}

#pragma mark - Main

- (void) playAllGames
{
    for (int i = 0; i < numberOfGames; i++)
    {
        [self readyNextGame];
        [self playGame];
    }
}

@end
